<?php

namespace frontend\models;

use common\helps\tools;
use Yii;
use yii\db\ActiveRecord;
use yii\behaviors\TimestampBehavior;
use yii\db\Exception;
use yii\db\Query;

/**
 * This is the model class for table "ProjectType".
 *
 */
class ProjectType extends ActiveRecord
{
    public $type;// 操作类型
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%project_type}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return[
            [['title'], 'trim'],
            [['id','title','status','type'], 'required', 'message'=>'{attribute}不能为空'],
            [['id','type','status'], 'match', 'pattern' => '/^[1-9][0-9]*$/','message'=>'{attribute}值无效'],
//            [['title'], 'unique', 'message'=>'{attribute}已存在'],
        ];
    }


    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => '项目类型id',
            'title' => '项目类型名称',
            'status'=>'项目类型状态',
            'type'=>'操作类型',
        ];
    }

    public function scenarios()
    {
        return [
            'default' => [
                'title',
                'status',
                'created_at',
                'updated_at',
                'deleted_at',
                'type',
            ],
            'update_status' => [
                'id',
                'type',
            ],
            'create' => [
                'title',
                'status',
                'created_at',
                'updated_at',
                'deleted_at',
            ],
            'update'=>[
                'id',
                'title',
                'status',
                'created_at',
                'updated_at',
                'deleted_at',
            ],
        ];
    }



    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            TimestampBehavior::className(),
        ];
    }


    /**
     * 获取项目类型列表
     * @param $params
     * @author hey_heyang(635870453@qq.com)
     * @return array
     */
    public function getProjectTypeList($params){
        if(!empty($params['title']) && !empty($params['status'])){
            $condition = ['and','status='.trim($params['status']),['like','title',$params['title']],['deleted_at'=>null]];
        }elseif(!empty($params['title'])){
            $condition = ['and',['like','title',$params['title']],['deleted_at'=>null]];
        }elseif(!empty($params['status'])){
            $condition =['and','status='.trim($params['status']),['deleted_at'=>null]];
        }else{
            $condition['deleted_at']=null;
        }
        $list=$this->find()->select('id,title,status')->where($condition)->asArray()->all();
        return empty($list)?array():$list;

    }



}
